package com.qf.front.controller;

import com.qf.front.biz.UserService;
import com.qf.ym.MyUtils;
import com.qf.ym.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

@Controller
public class UserController {

    @Autowired
    UserService userService;
    //登录
    @RequestMapping("/userlogin")
    public String userlogin(UserEntity ue, HttpServletRequest request){
         //用户名密码非空校验
        if(MyUtils.isEmpty(ue.getAacount())){
             request.setAttribute("erroyMsg1","用户名不能为空");
             return "login";
        }
        if(MyUtils.isEmpty(ue.getApass())){
            request.setAttribute("erroyMsg2","密码不能为空");
            return "login";
        }
         //md5加密密码
         ue.setApass(MyUtils.Md5Jm(ue.getApass()));
        //执行登录
         UserEntity   user = userService.login(ue);
        System.out.println(user);
         if(user==null){
             request.setAttribute("erroyMsg3","用户名或密码错误");
             return "login";
         }else {
             request.getSession().setAttribute("user",user);
             return "redirect:queryList";
         }

    }
    //注册
    @RequestMapping("regist")
    public String regist(UserEntity ue , HttpServletRequest request, MultipartFile headimage){
         String filename=MyUtils.getUUID()+headimage.getOriginalFilename();

         String realPath=request.getServletContext().getRealPath("images");


        File file = new File(realPath);
        if(!file.exists()){//文件不存在
            file.mkdir();
        }
        String path=realPath+File.separator+filename;//服务器中存储图片的真实全路径（包括文件名）-发布路径
        ue.setAimage("images/"+filename);
        try {

            InputStream is = headimage.getInputStream();
            FileOutputStream fos = new FileOutputStream(path);
            FileCopyUtils.copy(is,fos);

            request.setAttribute("success","文件上传成功");

        } catch (IOException e) {
            e.printStackTrace();
        }



        if(MyUtils.isEmpty(ue.getAacount())){
            request.setAttribute("erroyMsg1","用户名不能为空");
            return "regist";
        }
        if(MyUtils.isEmpty(ue.getApass())){
            request.setAttribute("erroyMsg2","密码不能为空");
            return "regist";
        }
        ue.setApass(MyUtils.Md5Jm(ue.getApass()));
        UserEntity user = userService.equalsaName(ue);
        if(user==null) {
            boolean result = userService.regist(ue);
            if (result) {
                request.setAttribute("success", "注册成功");
                return "login";
            } else {
                request.setAttribute("fail", "注册失败");
                return "regist";
            }
        }else {
            request.setAttribute("fail","用户存在，不可注册");
            return "regist";
        }

    }

    //注销
    @RequestMapping("exitUser")
    public String exitUser(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return "login";
    }
}
